package com.googlecode.perfectable.prosperable.entry;

import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Nullable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.io.ByteStreams;
import com.googlecode.perfectable.introspection.Resources;

// MARK resources servlet?
public class ResourcesServlet extends HttpServlet {
	private static final long serialVersionUID = -4609382746561846927L;

	@Override
	protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
			throws ServletException, IOException {
		if(req == null || resp == null) {
			throw new ServletException();
		}
		String path = req.getServletPath() + req.getPathInfo();
		path = path.substring(1); // MARK other request property?
		try(InputStream resource = Resources.load(path)) {
			if(resource == null) {
				resp.setStatus(404);
				return;
			}
			resp.setContentType("application/javascript"); // MARK constant
			resp.setStatus(200);
			ByteStreams.copy(resource, resp.getOutputStream());
		}
	}

}
